﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TiTi.tea.Api.Models;
using TiTi.tea.BLL;
using TiTi.tea.Comm;
using TiTi.tea.Model;

namespace TiTi.tea.Api.Controllers
{
    [RoutePrefix("api/Spu")]
    public class SpuController : ApiController
    {
        private SpuBLL bll = new SpuBLL();

        /// <summary>
        /// sku创建
        /// </summary>
        /// <param name="spuPara"></param>
        /// <returns></returns>
        [Route("")]
        [HttpPost]
        public Result Post(SpuVM spuPara)
        {
            if (bll.CreateSpu(spuPara.Spu, spuPara.Skus))
            {
                return Result.Success();
            }
            else
            {
                return Result.Error("添加失败");
            }
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <returns></returns>
        [Route("")]
        [HttpGet]
        public Result GetPage(string spuName="",int pageSize=10,int pageIndex=1)
        {
            var pagePara = new PagePara(pageSize, pageIndex);
            var data = bll.GetPage(spuName, pagePara);
            var total = pagePara.Total;
            return Result.Success(new {data,total});
        }

        /// <summary>
        /// 根据id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("GetById")]
        [HttpGet]
        public Result GetById(int id)
        {
            return Result.Success(bll.GetById(id));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Update")]
        [HttpPost]
        public Result Update(Spu param)
        {
            if(bll.Update(param))
            {
                return Result.Success();
            }
            else
            {
                return Result.Error("修改失败");
            }
        }
    }
}